import { getOrderDetail, orderPay, orderAgain, orderTake, orderDel } from '../../api/store.js';
import { getUserAccount } from '../../api/user.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': '订单详情',
      'color': true,
      'class': '0'
      // 'class': '2' 顶部为灰色
    },
    orderId: '',
    cartList: [],//购物车产品
    orderDetail: {},//订单详情
    isClose: false,
    payMode: [
      { name: "微信支付", icon: "icon-weixinzhifu", value: 'weixin', title: '微信快捷支付' },
    ],
    pay_close: false,
    pay_orderId: '',
    totalPrice: '0',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.orderId) this.setData({ orderId: options.orderId });
    if (options.isReturen) {
      this.setData({ 'parameter.class': '2' });
      this.selectComponent('#navbar').setClass();
    }
  },

  /**
   * 事件回调
   * 
  */
  onChangeFun: function (e) {
    let opt = e.detail;
    let action = opt.action || null;
    let value = opt.value != undefined ? opt.value : null;
    (action && this[action]) && this[action](value);
  },
  /**
   * 关闭支付组件
   * 
  */
  pay_close: function () {
    this.setData({ pay_close: false });
  },
  /**
   * 打开支付组件
   * 
  */
  pay_open: function () {
    this.setData({
      pay_close: true,
      pay_orderId: this.data.orderDetail.orderId,
      totalPrice: this.data.orderDetail.pay_price
    });
  },
  /**
   * 支付成功回调
   * 
  */
  pay_complete: function () {
    this.setData({ pay_close: false, pay_orderId: '' });
    this.getorderDetail();
  },
  /**
   * 支付失败回调
   * 
  */
  pay_fail: function () {
    this.setData({ pay_close: false, pay_orderId: '' });
  },
  /**
   * 登录授权回调
   * 
  */
  onLoadFun: function () {
    this.getorderDetail();
    this.getUserAccount();
  },
  /**
   * 获取用户账务信息
   * 
  */
  getUserAccount: function () {
    let that = this;
    getUserAccount().then(res => {
      that.data.payMode[1].number = res.data.money;
      that.setData({ payMode: that.data.payMode });
    })
  },
  /**
   * 获取订单详细信息
   * 
  */
  getorderDetail: function () {
    var that = this;
    wx.showLoading({ title: "正在加载中" });
    getOrderDetail(this.data.orderId).then(res => {
      wx.hideLoading();
      that.setData({
        orderDetail: res.data,
        cartList: res.data.cartList
      });
    }).catch(err => {
      wx.hideLoading();
      app.Tips({ title: err }, '/packageB/order_list/index');
    });
  },
  /**
   * 
   * 剪切订单号
  */
  copy: function () {
    var that = this;
    wx.setClipboardData({ data: this.data.orderDetail.orderId });
  },
  /**
   * 打电话
  */
  goTel: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.orderDetail.delivery_id
    })
  },
  /**
   * 再此购买
   * 
  */
  goOrderConfirm: function () {
    var that = this;
    orderAgain(that.data.orderDetail.orderId).then(res => {
      return wx.navigateTo({ url: '/packageB/order_confirm/index?cartId=' + res.data.cateId });
    });
  },
  confirmOrder: function () {
    var that = this;
    wx.showModal({
      title: '确认收货',
      content: '为保障权益，请收到货确认无误后，再确认收货',
      success: function (res) {
        if (res.confirm) {
          orderTake(that.data.orderId).then(res => {
            return app.Tips({ title: '操作成功', icon: 'success' }, function () {
              that.getorderDetail();
            });
          }).catch(err => {
            return app.Tips({ title: err });
          })
        }
      }
    })
  },
  /**
   * 
   * 删除订单
  */
  delOrder: function () {
    var that = this;
    orderDel(this.data.orderId).then(res => {
      return app.Tips({ title: '删除成功', icon: 'success' }, { tab: 3, url: 1 });
    }).catch(err => {
      return app.Tips({ title: err });
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.isLog && this.data.isClose) {
      this.getorderDetail();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({ isClose: true });
  },
})